<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="contact.domain.DAOContact"%>
<%@page import="contact.domain.Contact"%>
<%@page import="contact.domain.Enterprise"%>
<%@page import="contact.domain.PhoneNumber"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="contact.domain.DAOContactGroup"%>
<%@page import="contact.domain.ContactGroup"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@page import="org.springframework.context.ApplicationContext" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" media="screen" href="style.css">
<title>Update Contact</title>
</head>
<body>
<%

Long id = new Long (request.getParameter("id"));
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
DAOContact daoContact = (DAOContact) context.getBean("DAOContact");
Contact contact = daoContact.read(id);
String mobile = "";
String home = "";
String job = "";
for (PhoneNumber phone : contact.getProfiles()){
	if (phone.getPhoneKind().equals("MOBILE")) mobile = phone.getPhoneNumber();
	if (phone.getPhoneKind().equals("HOME")) home = phone.getPhoneNumber();
	if (phone.getPhoneKind().equals("JOB")) job = phone.getPhoneNumber();
}
boolean enterprise = false;
int numsiret = 0;

if (contact instanceof Enterprise){
	Enterprise e = (Enterprise) contact;
	numsiret = e.getNumSiret();
}
%>
<jsp:include page="menu.html" />
<form action="UpdateContact">
	<h1>Update Contact</h1>
	<input type="hidden" name="id" value="<%=contact.getId() %>"></input>		
	<p>
		<label for="firstname">First name</label>
		<input name="firstname" type="text" value="<%=contact.getFirstName() %>">		
	</p>
	<p>
		<label for="lastname">Last name</label>
		<input name="lastname" type="text" value="<%=contact.getLastName() %>"></input>		
	</p>
	<p>
		<label for="email">Email</label>
		<input name="email" type="text" value="<%=contact.getEmail() %>"></input>		
	</p>
	<h1></h1>
	<p>
		<label for="street">Street</label>
		<input name="street" type="text" value="<%=contact.getAddress().getStreet() %>"></input>		
	</p>
	<p>
		<label for="city">City</label>
		<input name="city" type="text" value="<%=contact.getAddress().getCity() %>"></input>		
	</p>
	<p>
		<label for="zip">Zip</label>
		<input name="zip" type="text" value="<%=contact.getAddress().getZip() %>"></input>		
	</p>
	<p>
		<label for="country">Country</label>
		<input name="country" type="text" value="<%=contact.getAddress().getCountry() %>"></input>		
	</p>
	<h1></h1>
	<p>
		<label for="mobile">Mobile</label>
		<input name="mobile" type="text" value="<%=mobile %>"/>		
	</p>
	<p>
		<label for="home">Home</label>
		<input name="home" type="text" value="<%=home %>" />		
	</p>
	<p>
		<label for="job">Job</label>
		<input name="job" type="text" value="<%=job %>" />		
	</p>
	<h1></h1>
	<p>
		<label for="enterprise">Enterprise</label>
		<input name="enterprise" value="true" type="checkbox" <%= enterprise? "checked": "" %> />		
	</p>
	<p>
		<label for="numsiret">Num. Siret</label>
		<input name="numsiret" type="text" value="<%=enterprise? ""+numsiret : "" %>" readonly/>		
	</p>
	<h1></h1>
	<p>
		<select name="group">
		<% 
		DAOContactGroup daoGroup = new DAOContactGroup();
		List<ContactGroup> groups = daoGroup.readAll();
		for(ContactGroup group : groups){
		%>
			<option value="<%= group.getGroupId()%>"><%= group.getGroupName()%></option>
		<% 
		}
		%>
		</select>	
	</p>
	<h2><input type="submit" value="Update"/><input type="reset" /></h2>
</form>
</body>
</html>